package com.example.pwj.ffstudy.ui.zhihu.presenter;

import com.example.pwj.ffstudy.base.BaseFragment;
import com.example.pwj.ffstudy.bean.zdaily.ZDailyComment;
import com.example.pwj.ffstudy.bean.zdaily.ZDailyCommentSection;
import com.example.pwj.ffstudy.ui.zhihu.contract.ZNewsCommContract;
import com.example.pwj.ffstudy.ui.zhihu.model.ZNewsCommModel;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by pwj on 2017/8/28.
 */

public class ZNewsCommPresenter implements ZNewsCommContract.Presenter {
    private ZNewsCommModel model;
    private ZNewsCommContract.View view;
    private int page;
    private static final int PAGE_SIZE = 8;

    @Inject
    ZNewsCommPresenter(ZNewsCommModel model) {
        this.model = model;
    }

    @Override
    public void takeView(ZNewsCommContract.View view) {
        this.view = view;
    }

    @Override
    public void dropView() {
        if (view == null) {
            view = null;
        }
    }

    /**
     * l
     * 获取初始长评论
     *
     * @param id           story_id
     * @param long_header  分页头部文字 ,18条长评论
     * @param short_header 分页头部文字 ,18条短评论
     */
    @Override
    public void loadZNewsLongComment(int id, String long_header, String short_header) {
        Observable<List<ZDailyComment>> observable = model.getZDailyLongComment(id);
        Single<List<ZDailyCommentSection>> single = model.convert(observable, long_header, short_header);
        single.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(((BaseFragment) view).<List<ZDailyCommentSection>>bindToLifecycle())
                .subscribe(new SingleObserver<List<ZDailyCommentSection>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onSuccess(List<ZDailyCommentSection> zDailyCommentSections) {
                        view.onLoadZNewsLongComment(zDailyCommentSections);
                        view.onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.onError();
                    }
                });
    }

    @Override
    public void loadZNewsShortComment(int id, final boolean isLoadMore) {
        if (!isLoadMore) {
            page = 1;
        } else {
            page++;
        }

        Observable<List<ZDailyComment>> observable = model.getZDailyShortComment(id);
        Observable<List<ZDailyComment>> listObservable = observable.take(PAGE_SIZE);
        Single<List<ZDailyCommentSection>> single = model.convert(listObservable,page,PAGE_SIZE);
        single.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(((BaseFragment) view).<List<ZDailyCommentSection>>bindToLifecycle())
                .subscribe(new SingleObserver<List<ZDailyCommentSection>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onSuccess(List<ZDailyCommentSection> zDailyCommentSections) {
                        if (!isLoadMore) {
                            view.onLoadZNewsShortComment(zDailyCommentSections);
                        } else {
                            view.onLoadMoreZNewsComment(zDailyCommentSections);
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        view.onError();
                    }
                });
    }
}
